import topHeader from '../top-header.vue'
import devicePieChart from './device-pie.vue'
import deviceOnlineRateChart from '../device-online-rate.vue'
import deviceAvailabilityRateChart from './device-availability-rate.vue'

import mapChart from './map.vue'
import agentModal from './agent-modal.vue'
import storeModal from '../store-modal.vue'
import siteModal from '../site-modal.vue'

import countTo from 'vue-count-to'
import { Request } from '@/utils'

export default {
    data () {
        return {
            mapData : {
                agentList : [], //代理商列表
                storeList : [], //门店列表
                deviceList : [], //设备列表
                siteList : [], //站点列表
            },

            currentAgentId : 0,
            currentStoreId : 0,
            currentSiteId : 0,

            completePaddintTop : false,
            showAgentModal : false,
            showStoreModal : false,
            showSiteModal : false,
            agentModalLeft : 0,
            agentModalTop : 0,

            yunyingTime : 0,
            totalChargeTime : 0,
            agentCount : 0, //代理数量
            storeCount : 0,//门店数量
            siteCount  : 0, //站点数量
            totalUserCount : 0, //总用户数
            todayRegisterUserCount : 0, //今天注册用户数
            latelyRegisterData : null, //近七天注册用户

            todayOrderCount : 0, //今日订单数
            totalOrderCount : 0, //总订单数
            latelyOrderData : null, //近七日订单


            deviceTotalCount : 0, //设备总数
            deviceCategoryData : null, //设备类型
            deviceOnlineRateData : null, //设备在线率
            deviceUseRateData : null, //设备利用率
        }
    },

    components : {
        topHeader,
        devicePieChart,
        deviceOnlineRateChart,
        deviceAvailabilityRateChart,
        mapChart,
        agentModal,
        storeModal,
        siteModal,

        countTo
    },

    methods : {
        mouseoverMap (e) {
            this.showSiteModal = false
            this.showStoreModal = false
            this.showAgentModal = false

            if(e.value) {
                var id = e.value[2]
                var type = e.value[3]
                console.log(type)
                if(type == 1) { //代理
                    this.currentAgentId = id
                    this.showAgentModal = true
                }else if(type == 2) { //门店
                    this.currentStoreId = id
                    this.showStoreModal = true
                }else if(type == 3) { //站点
                    this.currentSiteId = id
                    this.showSiteModal = true
                }
                
                this.$nextTick(() => {
                    var WH = document.documentElement.offsetHeight || document.body.offsetHeight
                    var H = this.$refs.agentModal.offsetHeight
                    var clientY = e.clientY
                    
                    if(clientY < H) {
                        clientY += (H - clientY + 10)
                    }

                    this.agentModalLeft = e.clientX
                    this.agentModalTop = clientY - H
                })
                
            }
            
        },

        run () {
            (new Request()).get('statistic/device/getPlatformOperateInfo', res => { //获取运营时间和充电时间
                if(res.success && res.data) {
                    this.yunyingTime = res.data.operateTime || 0
                    this.totalChargeTime = res.data.chargeTime * 60
                }
            });


            (new Request()).get('statistic/order/getOperateNum', res => { //代理商和门店的数量
                if(res.success && res.data) {
                    var data = typeof res.data == 'string' ? JSON.parse(res.data) : res.data
                    this.agentCount = Number(data.agentNum || 0)
                    this.storeCount = Number(data.storeNum || 0)
                    this.siteCount = Number(data.siteNum || 0)
                }
            });

            (new Request()).get('statistic/user/todayNewUsers', res => { //今日注册用户数
                if(res.success && res.data) {
                    this.todayRegisterUserCount = res.data.todayNewUsers || 0
                }
            });
            
            (new Request()).get('statistic/user/totalUserNum', res => { //总用户数
                if(res.success && res.data) {
                    this.totalUserCount = res.data
                }
            });

            (new Request()).get('statistic/user/totalSevenDaysUserRegisterNum', res => { //近七天注册用户
                var latelyRegisterData = {date : [], data : []}
                if(res.success && res.data) {
                   res.data.forEach(v => {
                       latelyRegisterData.date.push(v.date)
                       latelyRegisterData.data.push(v.registerNum)
                   })
                }

                this.latelyRegisterData = latelyRegisterData
            });

            (new Request()).get('statistic/order/todayOrders', res => { //今日订单订单数
                if(res.success && res.data) {
                    this.todayOrderCount = res.data.todayOrders || 0
                }
            });

            (new Request()).get('statistic/order/totalChargeOrderNum', res => { //总订单数
                if(res.success && res.data) {
                    this.totalOrderCount = res.data
                }
            });

            (new Request()).get('statistic/order/totalSevenDaysChargeOrderNum', res => { //获取近七天订单

                var latelyOrderData = {
                    date : [],
                    data : []
                }
    
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        latelyOrderData.date.push(item.date)
                        latelyOrderData.data.push(item.orderNum)
                    })
                }
    
                this.latelyOrderData = latelyOrderData
    
            });

            (new Request()).get('statistic/device/totalDeviceNum', res => { //获取设备数
                this.deviceTotalCount = res.data || 0
            });

            (new Request()).get('statistic/device/totalDeviceByVehicleType', res => { //获取设备
                var deviceCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        deviceCategoryData.push({name : this.$store.state.vehicleTypeList[item.vehicleType], value : item.deviceNum})
                    })
                }
                this.deviceCategoryData = deviceCategoryData
            });
			
			(new Request()).get('statistic/device/totalDeviceOnlineNum', res => { //获取设备在线率
			    var deviceOnlineRateData = []
			    if(res.success && res.data && res.data.length > 0) {
			        res.data.forEach(item => {
			            deviceOnlineRateData.push({name : this.$store.state.vehicleTypeList[item.vehicleType], value :  ((item.onlineNum || 0) / item.deviceNum).toFixed(1)})
			        })
			    }
			    this.deviceOnlineRateData = deviceOnlineRateData
			});

            (new Request()).get('statistic/device/totalDeviceUsageRates', res => { //获取设备利用率
                try {
                    var deviceUseRateData = {
                        name : [],
                        data : []
                    }
                    if(res.success && res.data) {
                        var data = res.data
                        deviceUseRateData.name.push(this.$store.state.vehicleTypeList.electrocar, this.$store.state.vehicleTypeList.car, this.$store.state.vehicleTypeList.chargeCabinet, this.$store.state.vehicleTypeList.exchangeCabinet)
                        deviceUseRateData.data.push(data.electrocar, data.car, data.chargeCabinet, data.exchangeCabinet)
                    }
                    this.deviceUseRateData = deviceUseRateData
                } catch (error) {

                }
            });



            (new Request()).get('statistic/device/getAgentList', res => { //获取代理商
                try {
                   if(res.success && res.data) {
                       var agentList = []
                       res.data.forEach(item => {
                            let longitude = Number(item.longitude || 0)
                            let latitude = Number(item.latitude || 0)
                            if(!longitude || !latitude) return
                            agentList.push({
                                longitude,
                                latitude,
                                name : item.name,
                                id : item.id
                            })
                       })

                       this.mapData.agentList = agentList
                   }
                } catch (error) {

                }
            });

            (new Request()).get('statistic/device/getStoreList', res => { //获取门店
                try {
                   if(res.success && res.data) {
                       var storeList = []
                       res.data.forEach(item => {
                            let longitude = Number(item.longitude || 0)
                            let latitude = Number(item.latitude || 0)
                            if(isNaN(longitude) || isNaN(latitude) || !longitude || !latitude) return
                            storeList.push({
                                longitude,
                                latitude,
                                name : item.name,
                                id : item.id
                            })
                       })
                       this.mapData.storeList = storeList
                   }
                } catch (error) {

                }
            });


            (new Request()).get('statistic/device/getSiteList', res => { //获取站点
                if(res.success && res.data) {
                    var siteList = []
                    res.data.forEach(item => {
                        var longitude = Number(item.longitude || 0)
                        var latitude = Number(item.latitude || 0)
                        if(isNaN(longitude) || isNaN(latitude) || !longitude || !latitude) return
                        siteList.push({longitude, latitude, name : item.name, id : item.id})
                    })
                    this.mapData.siteList = siteList
                }
            });


            (new Request()).get('statistic/device/getStatisticDeviceList', res => { //获取设备
                if(res.success && res.data) {
                    var deviceList = []
                    res.data.forEach(item => {
                        var longitude = Number(item.longitude || 0)
                        var latitude = Number(item.latitude || 0)
                        if(isNaN(longitude) || isNaN(latitude) || !longitude || !latitude) return
                        deviceList.push({longitude, latitude, name : item.deviceName, deviceNo :item.deviceNo, id : item.id})
                    })
                    this.mapData.deviceList = deviceList
                }
            });

            


            setTimeout(() => {
                this.run()
            }, 30 * 60 * 1000)
            
        }
    },

    mounted () {
        this.$refs.container.style.paddingTop = this.$refs.header.offsetHeight + 'px'
        this.completePaddintTop = true

        this.run()

    }
}